package com.person.title.service.impl;

import java.util.List;

import com.person.commons.utils.DAO;
import com.person.sys.model.SysFunc;
import com.person.title.model.Title;
import com.person.title.model.TitleClass;
import com.person.title.model.TitleFunc;
import com.person.title.model.TitleSub;
import com.person.title.service.TitleClassService;

public class TitleClassServiceImpl implements TitleClassService {

	private DAO dao ;
	private static final char SUCCESS = '1';
	private static final char FAIL = '0';
	private static final char FAIL_1 = '2';//主功能分类代码重复的提示返回
	private static final char FAIL_2 = '3';//主功能分类名字重复的提示返回
	
	@Override
	public char createTitleClass(TitleClass titleClass) {
		
		//主功能分类代码是否重复
		char forward = 0;
		String hql1 = "from TitleClass where classCode = '"+titleClass.getClassCode()+"'";
		List<TitleClass> list1 = dao.find(hql1);
		
		//主功能名字是否重复
		String hql2 = "from TitleClass where className = '"+titleClass.getClassName()+"'";
		List<TitleClass>  list2 = dao.find(hql2);

		if(list1.size() == 0 && list2.size() == 0){			
			this.dao.save(titleClass);
			forward = SUCCESS;
		}else{
			if(list1.size() > 0){
				forward = FAIL_1;
			}else if(list2.size() > 0){
				forward = FAIL_2;
			}
		}
		return forward;
	}

	@Override
	public char deleteTitleClass(TitleClass titleClass) {
		String hql = "from TitleFunc where classCodeGroup like '" + titleClass.getClassCode() + "%'";
	    List<TitleFunc> list = dao.find(hql);
	    if(list.size() == 0){
	    	this.dao.delete(titleClass) ;
	    	return SUCCESS;
	    }else{	    	
	    	return FAIL;
	    }	
	}

	@Override
	public List<TitleClass> findByCode(String classCode) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public TitleClass findSysClassId(String classId) {
		
		return (TitleClass) this.dao.find(TitleClass.class, classId);
	}

	@Override
	public List<TitleFunc> juniorTitleFuncList(String classId) {
		
		TitleClass titleClass = (TitleClass) dao.find(TitleClass.class, classId) ;	
		String hql = "from TitleFunc where classCodeGroup like '" + titleClass.getClassCode() + "__' order by classCodeGroup " ;
		List <TitleFunc> titleFuncs = this.dao.find(hql) ;
	
		return titleFuncs;
	}

	@Override
	public char modifyJuniorTitleFunc(TitleClass titleClass, String classCode) {
		String hql = "from TitleFunc where classCodeGroup like '" + classCode + "__%' order by classCodeGroup " ;
		List <TitleFunc> titleFuncs = this.dao.find(hql) ;
		for (TitleFunc titleFunc:titleFuncs) {
			String classCodeGroup = titleFunc.getClassCodeGroup() ;
			String classCodeGroup1 = titleClass.getClassCode() + classCodeGroup.substring(classCode.length(), classCodeGroup.length()) ;
			titleFunc.setClassCodeGroup(classCodeGroup1) ;
			this.dao.update(titleFunc) ;
		}
		return SUCCESS ;
	}

	@Override
	public char modifyTitleClass(TitleClass titleClass) {
		
		char forward = 0;
		
		//分类代码是否重复
		String hql1 = "from TitleClass where classCode = '"+titleClass.getClassCode()+"' and classId != '"+titleClass.getClassId()+"'";
		List<TitleClass> list1 = dao.find(hql1);
				
		//名字是否重复
		String hql2 = "from TitleClass where className = '"+titleClass.getClassName()+"' and classId != '"+titleClass.getClassId()+"'";
		List<TitleClass>  list2 = dao.find(hql2);
		
		if(list1.size() == 0 && list2.size() == 0 ) {
			
			dao.update(titleClass);
			
			forward = SUCCESS;
		}else{
			if(list1.size() > 0){
				forward = FAIL_1;
			}else if(list2.size() > 0){
				forward = FAIL_2;
			}
		}
		return forward;
	}

	@Override
	public List<TitleClass> titleClassList() {
		
		return this.dao.find("from TitleClass where 1=1 order by classCode") ;
	}
	
	@Override
	public TitleClass findTitleClassByCode(String classCode) {
		String hql = "from TitleClass where classCode = '" + classCode+ "'" ;
		List <TitleClass> titleClasses = this.dao.find(hql) ;
		TitleClass titleClass = null ;
		if (titleClasses.size()>0) {
			titleClass = (TitleClass) titleClasses.get(0) ;
		}
		return titleClass ;
	}
	
	

	public DAO getDao() {
		return dao;
	}

	public void setDao(DAO dao) {
		this.dao = dao;
	}

}
